/**
 * Thuc hien viec ket noi database
 */
package model.database;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author heroandtn3
 * 
 */
public class ConnectDB {

	private static Connection conn = null;
	private static String user = "root";
	private static String password = "12345";
	private static String database = "oop";
	private static String host = "localhost";
	private static String port = "3306";
	private static final String driver = "com.mysql.jdbc.Driver";

	/**
	 *  constructor ;)
	 */
	public ConnectDB() {
		// TODO Auto-generated constructor stub
		
	}

	/**
	 * @return the conn
	 */
	public static Connection getConn() {
		if (conn == null) {
			connect();
		}
		return conn;
	}

	/**
	 * @return the user
	 */
	public static String getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public static void setUser(String user) {
		ConnectDB.user = user;
	}

	/**
	 * @return the password
	 */
	public static String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public static void setPassword(String password) {
		ConnectDB.password = password;
	}

	/**
	 * @return the database
	 */
	public static String getDatabase() {
		return database;
	}

	/**
	 * @param database the database to set
	 */
	public static void setDatabase(String database) {
		ConnectDB.database = database;
	}

	/**
	 * @return the host
	 */
	public static String getHost() {
		return host;
	}

	/**
	 * @param host the host to set
	 */
	public static void setHost(String host) {
		ConnectDB.host = host;
	}

	/**
	 * @return the port
	 */
	public static String getPort() {
		return port;
	}

	/**
	 * @param port the port to set
	 */
	public static void setPort(String port) {
		ConnectDB.port = port;
	}
	
	/**
	 * Ket noi vao CSDL
	 * Tra ve true neu ket noi thanh cong
	 * Nguoc lai tra ve false 
	 */
	public static boolean connect() {
		try {
			Class.forName(driver).newInstance();
			conn = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port+ "/" + database, user, password);
		} catch (Exception e) {
			//e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * Dong ket noi vao CSDL
	 * Tra ve true neu dong thanh cong
	 * Nguoc lai tra ve false
	 */
	public static boolean closeConnect() {
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * Doc du lieu ket noi toi co so du lieu
	 * Nguon: suu tam
	 */
    public static void readConfig() {
        try {
            File fXmlFile = new File("config.xml");
            if (fXmlFile.exists()) {
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(fXmlFile);
                doc.getDocumentElement().normalize();

                NodeList nList = doc.getElementsByTagName("connection");

                for (int temp = 0; temp < nList.getLength(); temp++) {

                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                        Element eElement = (Element) nNode;

                        host = getTagValue("hostname", eElement);
                        port = getTagValue("port", eElement);
                        user = getTagValue("username", eElement);
                        password = getTagValue("password", eElement);
                        database = getTagValue("database", eElement);

                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Lay gia tri theo tag name
    private static String getTagValue(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

        Node nValue = (Node) nlList.item(0);

        return nValue.getNodeValue();
    }

}
